{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4f2a9497",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: requests in e:\\anaconda3\\lib\\site-packages (2.26.0)\n",
      "Requirement already satisfied: idna<4,>=2.5 in e:\\anaconda3\\lib\\site-packages (from requests) (3.2)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in e:\\anaconda3\\lib\\site-packages (from requests) (1.26.7)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in e:\\anaconda3\\lib\\site-packages (from requests) (2021.10.8)\n",
      "Requirement already satisfied: charset-normalizer~=2.0.0 in e:\\anaconda3\\lib\\site-packages (from requests) (2.0.4)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "798d5954",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "from "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b35f34cb",
   "metadata": {},
   "source": [
    "设置API Key 和API Secret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a1e9805c",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY='https://api-cn.faceplusplus.com/facepp/v1/beautify'\n",
    "API_SECRET='LQ4BvNK3UpaedTgdTge-QPpHad_hifV_'\n",
    "URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "526d94cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "IMAGE_URL =r\"C:\\Users\\yly\\Pictures\\li.jpg\"\n",
    "files = {\"image_file\": open(IMAGE_URL, \"rb\")}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "abd6d1f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请求参数\n",
    "data = {\"api_key\":API_KEY, \n",
    "        \"api_secret\":API_SECRET, \n",
    "        \"return_attributes\": \"gender,age,smiling,beauty\",\n",
    "        \"return_landmark\":1}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bfb98cc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 发送请求\n",
    "response = requests.post(URL,data=data,files=files)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e11ce3f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<Response [401]>\n"
     ]
    }
   ],
   "source": [
    "req_con = response\n",
    "print(req_con)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13b917ca",
   "metadata": {},
   "source": [
    " API 返回了一个 JSON 格式的响应时，你可以使用 response.json() 方法来轻松地将这个 JSON 字符串转换为 Python 对象，这样你就可以更方便地在 Python 代码中处理这些数据了。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "27648a6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'error_message': 'AUTHENTICATION_ERROR'}\n"
     ]
    }
   ],
   "source": [
    "req_con = response.json()\n",
    "print(req_con)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f442adeb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"request_id\": \"1717158476,f54a5d93-5fc0-4a63-9187-1a9c8d545444\",\n",
      "    \"time_used\": 167,\n",
      "    \"faces\": [\n",
      "        {\n",
      "            \"face_token\": \"dab3dee2694fe5305b99f95d14b5cfc2\",\n",
      "            \"face_rectangle\": {\n",
      "                \"top\": 133,\n",
      "                \"left\": 108,\n",
      "                \"width\": 206,\n",
      "                \"height\": 206\n",
      "            },\n",
      "            \"landmark\": {\n",
      "                \"contour_chin\": {\n",
      "                    \"x\": 212,\n",
      "                    \"y\": 340\n",
      "                },\n",
      "                \"contour_left1\": {\n",
      "                    \"x\": 118,\n",
      "                    \"y\": 162\n",
      "                },\n",
      "                \"contour_left2\": {\n",
      "                    \"x\": 114,\n",
      "                    \"y\": 187\n",
      "                },\n",
      "                \"contour_left3\": {\n",
      "                    \"x\": 112,\n",
      "                    \"y\": 211\n",
      "                },\n",
      "                \"contour_left4\": {\n",
      "                    \"x\": 113,\n",
      "                    \"y\": 237\n",
      "                },\n",
      "                \"contour_left5\": {\n",
      "                    \"x\": 119,\n",
      "                    \"y\": 261\n",
      "                },\n",
      "                \"contour_left6\": {\n",
      "                    \"x\": 130,\n",
      "                    \"y\": 282\n",
      "                },\n",
      "                \"contour_left7\": {\n",
      "                    \"x\": 146,\n",
      "                    \"y\": 301\n",
      "                },\n",
      "                \"contour_left8\": {\n",
      "                    \"x\": 164,\n",
      "                    \"y\": 318\n",
      "                },\n",
      "                \"contour_left9\": {\n",
      "                    \"x\": 185,\n",
      "                    \"y\": 333\n",
      "                },\n",
      "                \"contour_right1\": {\n",
      "                    \"x\": 313,\n",
      "                    \"y\": 175\n",
      "                },\n",
      "                \"contour_right2\": {\n",
      "                    \"x\": 314,\n",
      "                    \"y\": 198\n",
      "                },\n",
      "                \"contour_right3\": {\n",
      "                    \"x\": 312,\n",
      "                    \"y\": 222\n",
      "                },\n",
      "                \"contour_right4\": {\n",
      "                    \"x\": 308,\n",
      "                    \"y\": 245\n",
      "                },\n",
      "                \"contour_right5\": {\n",
      "                    \"x\": 301,\n",
      "                    \"y\": 267\n",
      "                },\n",
      "                \"contour_right6\": {\n",
      "                    \"x\": 288,\n",
      "                    \"y\": 287\n",
      "                },\n",
      "                \"contour_right7\": {\n",
      "                    \"x\": 273,\n",
      "                    \"y\": 304\n",
      "                },\n",
      "                \"contour_right8\": {\n",
      "                    \"x\": 256,\n",
      "                    \"y\": 319\n",
      "                },\n",
      "                \"contour_right9\": {\n",
      "                    \"x\": 237,\n",
      "                    \"y\": 334\n",
      "                },\n",
      "                \"left_eye_bottom\": {\n",
      "                    \"x\": 173,\n",
      "                    \"y\": 168\n",
      "                },\n",
      "                \"left_eye_center\": {\n",
      "                    \"x\": 174,\n",
      "                    \"y\": 164\n",
      "                },\n",
      "                \"left_eye_left_corner\": {\n",
      "                    \"x\": 153,\n",
      "                    \"y\": 162\n",
      "                },\n",
      "                \"left_eye_lower_left_quarter\": {\n",
      "                    \"x\": 163,\n",
      "                    \"y\": 165\n",
      "                },\n",
      "                \"left_eye_lower_right_quarter\": {\n",
      "                    \"x\": 184,\n",
      "                    \"y\": 169\n",
      "                },\n",
      "                \"left_eye_pupil\": {\n",
      "                    \"x\": 178,\n",
      "                    \"y\": 163\n",
      "                },\n",
      "                \"left_eye_right_corner\": {\n",
      "                    \"x\": 194,\n",
      "                    \"y\": 169\n",
      "                },\n",
      "                \"left_eye_top\": {\n",
      "                    \"x\": 175,\n",
      "                    \"y\": 157\n",
      "                },\n",
      "                \"left_eye_upper_left_quarter\": {\n",
      "                    \"x\": 164,\n",
      "                    \"y\": 158\n",
      "                },\n",
      "                \"left_eye_upper_right_quarter\": {\n",
      "                    \"x\": 186,\n",
      "                    \"y\": 161\n",
      "                },\n",
      "                \"left_eyebrow_left_corner\": {\n",
      "                    \"x\": 130,\n",
      "                    \"y\": 139\n",
      "                },\n",
      "                \"left_eyebrow_lower_left_quarter\": {\n",
      "                    \"x\": 148,\n",
      "                    \"y\": 140\n",
      "                },\n",
      "                \"left_eyebrow_lower_middle\": {\n",
      "                    \"x\": 167,\n",
      "                    \"y\": 142\n",
      "                },\n",
      "                \"left_eyebrow_lower_right_quarter\": {\n",
      "                    \"x\": 185,\n",
      "                    \"y\": 146\n",
      "                },\n",
      "                \"left_eyebrow_right_corner\": {\n",
      "                    \"x\": 204,\n",
      "                    \"y\": 146\n",
      "                },\n",
      "                \"left_eyebrow_upper_left_quarter\": {\n",
      "                    \"x\": 148,\n",
      "                    \"y\": 130\n",
      "                },\n",
      "                \"left_eyebrow_upper_middle\": {\n",
      "                    \"x\": 168,\n",
      "                    \"y\": 132\n",
      "                },\n",
      "                \"left_eyebrow_upper_right_quarter\": {\n",
      "                    \"x\": 187,\n",
      "                    \"y\": 136\n",
      "                },\n",
      "                \"mouth_left_corner\": {\n",
      "                    \"x\": 167,\n",
      "                    \"y\": 255\n",
      "                },\n",
      "                \"mouth_lower_lip_bottom\": {\n",
      "                    \"x\": 215,\n",
      "                    \"y\": 298\n",
      "                },\n",
      "                \"mouth_lower_lip_left_contour1\": {\n",
      "                    \"x\": 188,\n",
      "                    \"y\": 275\n",
      "                },\n",
      "                \"mouth_lower_lip_left_contour2\": {\n",
      "                    \"x\": 177,\n",
      "                    \"y\": 275\n",
      "                },\n",
      "                \"mouth_lower_lip_left_contour3\": {\n",
      "                    \"x\": 192,\n",
      "                    \"y\": 290\n",
      "                },\n",
      "                \"mouth_lower_lip_right_contour1\": {\n",
      "                    \"x\": 241,\n",
      "                    \"y\": 278\n",
      "                },\n",
      "                \"mouth_lower_lip_right_contour2\": {\n",
      "                    \"x\": 251,\n",
      "                    \"y\": 279\n",
      "                },\n",
      "                \"mouth_lower_lip_right_contour3\": {\n",
      "                    \"x\": 237,\n",
      "                    \"y\": 293\n",
      "                },\n",
      "                \"mouth_lower_lip_top\": {\n",
      "                    \"x\": 216,\n",
      "                    \"y\": 284\n",
      "                },\n",
      "                \"mouth_right_corner\": {\n",
      "                    \"x\": 260,\n",
      "                    \"y\": 260\n",
      "                },\n",
      "                \"mouth_upper_lip_bottom\": {\n",
      "                    \"x\": 217,\n",
      "                    \"y\": 264\n",
      "                },\n",
      "                \"mouth_upper_lip_left_contour1\": {\n",
      "                    \"x\": 208,\n",
      "                    \"y\": 254\n",
      "                },\n",
      "                \"mouth_upper_lip_left_contour2\": {\n",
      "                    \"x\": 187,\n",
      "                    \"y\": 253\n",
      "                },\n",
      "                \"mouth_upper_lip_left_contour3\": {\n",
      "                    \"x\": 192,\n",
      "                    \"y\": 259\n",
      "                },\n",
      "                \"mouth_upper_lip_right_contour1\": {\n",
      "                    \"x\": 228,\n",
      "                    \"y\": 256\n",
      "                },\n",
      "                \"mouth_upper_lip_right_contour2\": {\n",
      "                    \"x\": 245,\n",
      "                    \"y\": 257\n",
      "                },\n",
      "                \"mouth_upper_lip_right_contour3\": {\n",
      "                    \"x\": 239,\n",
      "                    \"y\": 262\n",
      "                },\n",
      "                \"mouth_upper_lip_top\": {\n",
      "                    \"x\": 218,\n",
      "                    \"y\": 257\n",
      "                },\n",
      "                \"nose_contour_left1\": {\n",
      "                    \"x\": 206,\n",
      "                    \"y\": 172\n",
      "                },\n",
      "                \"nose_contour_left2\": {\n",
      "                    \"x\": 196,\n",
      "                    \"y\": 213\n",
      "                },\n",
      "                \"nose_contour_left3\": {\n",
      "                    \"x\": 202,\n",
      "                    \"y\": 238\n",
      "                },\n",
      "                \"nose_contour_lower_middle\": {\n",
      "                    \"x\": 220,\n",
      "                    \"y\": 246\n",
      "                },\n",
      "                \"nose_contour_right1\": {\n",
      "                    \"x\": 239,\n",
      "                    \"y\": 175\n",
      "                },\n",
      "                \"nose_contour_right2\": {\n",
      "                    \"x\": 245,\n",
      "                    \"y\": 216\n",
      "                },\n",
      "                \"nose_contour_right3\": {\n",
      "                    \"x\": 237,\n",
      "                    \"y\": 241\n",
      "                },\n",
      "                \"nose_left\": {\n",
      "                    \"x\": 187,\n",
      "                    \"y\": 227\n",
      "                },\n",
      "                \"nose_right\": {\n",
      "                    \"x\": 251,\n",
      "                    \"y\": 232\n",
      "                },\n",
      "                \"nose_tip\": {\n",
      "                    \"x\": 222,\n",
      "                    \"y\": 231\n",
      "                },\n",
      "                \"right_eye_bottom\": {\n",
      "                    \"x\": 270,\n",
      "                    \"y\": 173\n",
      "                },\n",
      "                \"right_eye_center\": {\n",
      "                    \"x\": 270,\n",
      "                    \"y\": 170\n",
      "                },\n",
      "                \"right_eye_left_corner\": {\n",
      "                    \"x\": 248,\n",
      "                    \"y\": 173\n",
      "                },\n",
      "                \"right_eye_lower_left_quarter\": {\n",
      "                    \"x\": 259,\n",
      "                    \"y\": 173\n",
      "                },\n",
      "                \"right_eye_lower_right_quarter\": {\n",
      "                    \"x\": 281,\n",
      "                    \"y\": 172\n",
      "                },\n",
      "                \"right_eye_pupil\": {\n",
      "                    \"x\": 269,\n",
      "                    \"y\": 168\n",
      "                },\n",
      "                \"right_eye_right_corner\": {\n",
      "                    \"x\": 291,\n",
      "                    \"y\": 170\n",
      "                },\n",
      "                \"right_eye_top\": {\n",
      "                    \"x\": 270,\n",
      "                    \"y\": 163\n",
      "                },\n",
      "                \"right_eye_upper_left_quarter\": {\n",
      "                    \"x\": 258,\n",
      "                    \"y\": 166\n",
      "                },\n",
      "                \"right_eye_upper_right_quarter\": {\n",
      "                    \"x\": 281,\n",
      "                    \"y\": 165\n",
      "                },\n",
      "                \"right_eyebrow_left_corner\": {\n",
      "                    \"x\": 246,\n",
      "                    \"y\": 149\n",
      "                },\n",
      "                \"right_eyebrow_lower_left_quarter\": {\n",
      "                    \"x\": 263,\n",
      "                    \"y\": 150\n",
      "                },\n",
      "                \"right_eyebrow_lower_middle\": {\n",
      "                    \"x\": 280,\n",
      "                    \"y\": 149\n",
      "                },\n",
      "                \"right_eyebrow_lower_right_quarter\": {\n",
      "                    \"x\": 295,\n",
      "                    \"y\": 150\n",
      "                },\n",
      "                \"right_eyebrow_right_corner\": {\n",
      "                    \"x\": 309,\n",
      "                    \"y\": 151\n",
      "                },\n",
      "                \"right_eyebrow_upper_left_quarter\": {\n",
      "                    \"x\": 263,\n",
      "                    \"y\": 140\n",
      "                },\n",
      "                \"right_eyebrow_upper_middle\": {\n",
      "                    \"x\": 280,\n",
      "                    \"y\": 137\n",
      "                },\n",
      "                \"right_eyebrow_upper_right_quarter\": {\n",
      "                    \"x\": 297,\n",
      "                    \"y\": 139\n",
      "                }\n",
      "            },\n",
      "            \"attributes\": {\n",
      "                \"gender\": {\n",
      "                    \"value\": \"Female\"\n",
      "                },\n",
      "                \"age\": {\n",
      "                    \"value\": 22\n",
      "                },\n",
      "                \"smile\": {\n",
      "                    \"value\": 100.0,\n",
      "                    \"threshold\": 50.0\n",
      "                },\n",
      "                \"beauty\": {\n",
      "                    \"male_score\": 79.659,\n",
      "                    \"female_score\": 81.008\n",
      "                }\n",
      "            }\n",
      "        }\n",
      "    ],\n",
      "    \"image_id\": \"+N70F8AfOnSQOlanCfALWg==\",\n",
      "    \"face_num\": 1\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "req_con = response.json()\n",
    "json.dumps(req_con, indent=4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "433bbed4",
   "metadata": {},
   "source": [
    "用“items”迭代处理字典；用“in”检查成员关系。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e9ff8a04",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 提取人脸坐标信息\n",
    "faces = req_con.get('faces', [])\n",
    "face_coordinates = []\n",
    "\n",
    "for face in faces:\n",
    "   \n",
    "    face_info = {\n",
    "        'face_token': face.get('face_token'),\n",
    "        'landmark': face.get('landmark'),\n",
    "        'attributes':face.get('attributes')\n",
    "    }\n",
    "  \n",
    "    face_coordinates.append(face_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cb6f8876",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存人脸坐标字典到文件\n",
    "output_file = 'face_coordinates.json'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "07586a69",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(output_file, 'w') as f:\n",
    "    json.dump(face_coordinates, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c26ebafb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
